package com.liveinstars.im.core.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.liveinstars.api.dto.BatchImGetUserReq;
import com.liveinstars.api.dto.ImGetUserInfo;
import com.liveinstars.api.facade.ImUserCentreApi;
import com.liveinstars.im.domain.dao.model.RecommendFriend;
import com.liveinstars.im.domain.manager.RecommendFriendManager;
import com.liveinstars.api.im.query.RecommendFriendQuery;
import com.liveinstars.api.im.vo.RecommendFriendVO;
import com.liveinstars.im.core.service.RecommendFriendService;
import com.onepiece.shipelves.common.Result;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhouze
 * @date: 2020/3/5
 */
@Service("recommendFriendService")
public class RecommendFriendServiceImpl implements RecommendFriendService {

    @Autowired
    private RecommendFriendManager recommendFriendManager;

    @Autowired
    private ImUserCentreApi imUserCentreApi;

    @Override
    public void add(Long userId) {
        RecommendFriend recommendFriend = new RecommendFriend();
        recommendFriend.setUserId(userId);
        recommendFriendManager.add(recommendFriend);
    }

    @Override
    public List<RecommendFriendVO> listAll() {
        List<RecommendFriendVO> list = Lists.newArrayList();
        RecommendFriendQuery query = new RecommendFriendQuery();
        List<RecommendFriend> recommendFriendList = recommendFriendManager.listByQuery(query);
        if (CollectionUtils.isNotEmpty(recommendFriendList)) {
            List<Long> userIdList = recommendFriendList.stream().map(recommendFriend -> recommendFriend.getUserId()).collect(Collectors.toList());
            BatchImGetUserReq req = new BatchImGetUserReq();
            req.setUids(userIdList);
            Result<List<ImGetUserInfo>> listResult = imUserCentreApi.batchImGetUserInfo(req);
            Map<Long, ImGetUserInfo> userMap = Maps.newHashMap();
            if (listResult.getCode().equals(Result.SUCCESS)) {
                listResult.getData().forEach(user -> userMap.put(user.getUid(), user));
            }
            list = recommendFriendList.stream().map(recommendFriend -> {
                RecommendFriendVO vo = new RecommendFriendVO();
                vo.setId(recommendFriend.getId());
                vo.setUserId(recommendFriend.getUserId());
                ImGetUserInfo userInfo = userMap.get(recommendFriend.getUserId());
                if (userMap.containsKey(recommendFriend.getUserId())) {
                    vo.setUserName(userInfo.getUserName());
                    vo.setIndividualResume(userInfo.getIndividualResume());
                    vo.setLogo(userInfo.getLogo());
                    vo.setCreateTime(Long.valueOf(userInfo.getRegdate()));
                    vo.setGroupId(userInfo.getGroupId());
                }
                vo.setSort(recommendFriend.getSort());
                return vo;
            }).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public void updateSortById(Long id, Integer sort) {
        recommendFriendManager.updateSortById(id, sort);
    }

    @Override
    public void deleteById(Long id) {
        recommendFriendManager.removeById(id);
    }
}
